<?php


namespace App\Http\Controllers\Business;


use App\Http\Controllers\Controller;
use App\Models\Business\BusinessClassification;
use App\Models\Business\BusinessClassificationFirst;
use App\Models\Business\BusinessClassificationSecond;
use App\Models\Business\BusinessClassificationThird;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Validator;

/**
 * @group 商家分类
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class BusinessClassificationController extends Controller
{
    /**
     * 商家分类查看
     *
     * @queryParam  name 分类名字
     * @queryParam  pid 分类父类
     *
     * @bodyParam  pid int  分类id
     * @bodyParam  level int  分类等级 1 1级分类  2 2级分类 3 3级分类
     */
    public function index(Request $request)
    {

        if ($request->get('level') == 1) {
            $query = BusinessClassificationFirst::where(function ($q) use ($request) {
                $request->has('pid') && $q->where('pid', $request->get('pid'));
            })->get()->toArray();
        } else if ($request->get('level') == 2) {
            $query = BusinessClassificationSecond::with('third')->where(function ($q) use ($request) {
                $request->has('pid') && $q->where('pid', $request->get('pid'));
            })->get()->toArray();
        } else if ($request->get('level') == 3) {
            $query = BusinessClassificationThird::where(function ($q) use ($request) {
                $request->has('pid') && $q->where('pid', $request->get('pid'));
            })->get()->toArray();
        } else {
            $query = BusinessClassificationFirst::with('second')->where(function ($q) use ($request) {
                $request->has('pid') && $q->where('pid', $request->get('pid'));
            })->get()->toArray();
        }
        return response()->json($query, 200);
    }

    /**
     * 商家分类信息查看单条
     *
     * @queryParam  name 分类名字
     *
     * @bodyParam  level int required 分类等级 1 1级分类  2 2级分类 3 3级分类
     *
     * 503 没有传对应等级分类
     *
     */
    public function show(Request $request, $pk)
    {

        if ($request->get('level') == 1) {
            $data = BusinessClassificationFirst::find($pk);
        } else if ($request->get('level') == 2) {
            $data = BusinessClassificationSecond::find($pk);
        } else if ($request->get('level') == 3) {
            $data = BusinessClassificationThird::find($pk);
        } else {
            return response()->json(["message" => "没有传对应等级分类"], 503);
        }
        if ($data == []) {
            return response()->json(["message" => "没有该信息"], 404);
        } else {
            return response()->json($data, 200);
        }
    }

    /**
     * 商家分类添加
     *
     * @bodyParam  name string required 分类名字
     * @bodyParam  pid int required 分类上级
     * @bodyParam  level int required 分类等级 1 1级分类  2 2级分类 3 3级分类

     * 503 添加分类失败
     * 504 添加失败
     */
    public function store(Request $request)
    {

        try {
            $e = genValidate();
            $validator = Validator::make($request->all(), $e);
            if ($validator->fails()) {
                return response()->json($validator->errors()->messages(), 400);
            }
            $row = $request->all();
            unset($row['level']);
            if ($request->get('level') == 1) {
                $data = BusinessClassificationFirst::insertGetId($row);
            } else if ($request->get('level') == 2) {

                $data = BusinessClassificationSecond::insertGetId($row);
            } else if ($request->get('level') == 3) {
                $data = BusinessClassificationThird::insertGetId($row);
            }
            if (!$data) {
                return response()->json(["message" => "添加失败"], 503);
            }
            return response()->json(["message" => "添加成功"], 200);
        } catch (QueryException $e) {
            return response()->json(["message" => "添加失败"], 504);
        }
    }

    /**
     * 商家分类修改
     *
     * @bodyParam  name string  分类名字
     * @bodyParam  pid int  分类上级
     * @bodyParam  level int required 分类等级 1 1级分类  2 2级分类 3 3级分类
     *

     * 503 没有传对应等级分类
     * 504 没有找到对应分类
     * 505 修改失败
     */
    public function update(Request $request, $pk)
    {
        $e = genValidate();
        $validator = Validator::make($request->all(), $e);
        if ($validator->fails()) {
            return response()->json($validator->errors()->messages(), 400);
        }
        $row = $request->all();
        if ($request->get('level') == 1) {
            $data = BusinessClassificationFirst::find($pk);
        } else if ($request->get('level') == 2) {
            $data = BusinessClassificationSecond::find($pk);
        } else if ($request->get('level') == 3) {
            $data = BusinessClassificationThird::find($pk);
        } else {
            return response()->json(["message" => "没有传对应等级分类"], 503);
        }
        if (!$data) {
            return response()->json(["message" => "没有找到对应分类"], 504);
        }
        unset($row['level']);
        try {
            $data->update($row);
            return response()->json(["message" => "修改成功"], 200);
        } catch (QueryException $e) {
            return response()->json(["message" => "修改失败"], 505);
        }
    }

    /**
     * 商家分类删除
     * @bodyParam  level int required 分类等级 1 1级分类  2 2级分类 3 3级分类
     *

     * 503 没有传对应等级分类
     * 504 分类下存在子分类
     */
    public function destroy(Request $request, $pk)
    {

        if ($request->get('level') == 1) {
            $data = BusinessClassificationFirst::find($pk);
        } else if ($request->get('level') == 2) {
            $data = BusinessClassificationSecond::find($pk);
        } else if ($request->get('level') == 3) {
            $data = BusinessClassificationThird::find($pk);
        } else {
            return response()->json(["message" => "没有传对应等级分类"], 503);
        }
        if ($data == []) {
            return response()->json(["message" => "没有该信息"], 404);
        }

        try{
            $data->delete();
            return response()->json(["message" => "删除成功"], 200);
        } catch (\Exception $e) {

            return response()->json(["message" => "分类下存在子分类"], 504);
        }

    }

}
